package com.hyk.commonLib;

import android.app.Application;
import android.graphics.drawable.Drawable;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatDelegate;

import com.airbnb.lottie.Lottie;
import com.airbnb.lottie.LottieConfig;
import com.hyk.commonLib.common.utils.ActivityManageUtils;
import com.hyk.commonLib.common.utils.AppUtils;
import com.hyk.commonLib.common.utils.BusUtils;
import com.hyk.commonLib.common.utils.FileUtils;
import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.dataCache.BaseDataCacheUtils;
import com.hyk.commonLib.common.utils.netRequest.DefaultNetUtilsImpl;
import com.hyk.commonLib.common.utils.netRequest.INetUtils;
import com.hyk.commonLib.common.utils.netRequest.NetUtils;

import java.io.File;
import java.util.function.Supplier;

/**
 * Created by 贺玉琨 on 2019/3/26.
 */
public class LibInitializer {
    //    private static boolean useCustomFont = true;
    @LayoutRes
    private static int LOADING_DIALOG_LAYOUT_ID;
    private static Supplier<Drawable> PLACEHOLDER_SUPPLIER = () -> ResUtils.getDrawable(R.drawable.common_lib_img_placeholder);

    public static void init(Application application) {
        AppUtils.init(application);
        if (NetUtils.netUtilsImplIsEmpty()) {
            NetUtils.setNetUtilsImpl(new DefaultNetUtilsImpl());
        }
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        BusUtils.init();
        BaseDataCacheUtils.init();
        ActivityManageUtils.init();
        File lottieCacheDir = new File(FileUtils.getCacheDir(), "__commonLib_lottie__");
        if (!lottieCacheDir.exists()){
            lottieCacheDir.mkdirs();
        }
        Lottie.initialize(new LottieConfig.Builder().setNetworkCacheDir(lottieCacheDir).build());
//        if (useCustomFont) FontUtils.init();
    }

//    public static void useCustomFont(boolean useCustomFont) {
//        LibInitializer.useCustomFont = useCustomFont;
//    }
//
//    public static void setCustomFont(String fontPath) {
//        FontUtils.setFontPath(fontPath);
//        LibInitializer.useCustomFont = true;
//    }

    public static void setNetUtilsImpl(INetUtils iNetUtils) {
        NetUtils.setNetUtilsImpl(iNetUtils);
    }

    public static void setLoadingDialogLayoutId(@LayoutRes int loadingDialogLayoutId) {
        LOADING_DIALOG_LAYOUT_ID = loadingDialogLayoutId;
    }

    public static int getLoadingDialogLayoutId() {
        return LOADING_DIALOG_LAYOUT_ID;
    }

    public static boolean isConfigLoadingDialogLayout() {
        return getLoadingDialogLayoutId() != 0;
    }

    public static void setPlaceholderSupplier(Supplier<Drawable> supplier) {
        PLACEHOLDER_SUPPLIER = supplier;
    }

    public static Drawable getLoadImgPlaceholder() {
        if (PLACEHOLDER_SUPPLIER == null) return null;
        return PLACEHOLDER_SUPPLIER.get();
    }
}